package se19team1e.gui.awt;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import se19team1e.controller.Application;
import se19team1e.controller.ViewJobResponseDetailController;
import se19team1e.controller.ViewJobResponseResultController;
import se19team1e.entity.JobResponseDTO;
import se19team1e.gui.JobAdvertResponseResultScreen;
import se19team1e.util.Constants;
import se19team1e.util.DateUtils;
import se19team1e.util.ReflectionUtils;
/**
 * @author Tay Toh Tong
 */



/**
* @author Tay Toh Tong
*/
public class AWTJobAdvertResponseResultsScreen extends AWTBaseScreen implements JobAdvertResponseResultScreen {

	private ViewJobResponseResultController controller;
	private JLabel titleLabel;
	private JButton viewJobResponseButton;
	private JButton backButton;
	private JTable resultTable;
	private JPanel ResulttablePanel;
	private JPanel buttonPanel;
	private JPanel titlePanel;
	private JPanel MainPanel;

	public AWTJobAdvertResponseResultsScreen(Application application) {
		super(application);
		
		MainPanel = new JPanel();
		getContentPane().add(MainPanel);
		BorderLayout MainPanelLayout = new BorderLayout();
		MainPanel.setLayout(MainPanelLayout);
		MainPanel.setBounds(0, 0, 584, 364);
		{
			titlePanel = new JPanel();
			MainPanel.add(titlePanel, BorderLayout.NORTH);
			BorderLayout titlePanelLayout = new BorderLayout();
			titlePanel.setLayout(titlePanelLayout);
			{
				titleLabel = new JLabel();
				titlePanel.add(titleLabel, BorderLayout.CENTER);
				titleLabel.setText("Job Response");
				titleLabel.setLayout(null);
				titleLabel.setPreferredSize(new java.awt.Dimension(516, 20));
			}
			{
				backButton = new JButton();
				titlePanel.add(backButton, BorderLayout.EAST);
				backButton.setText("Back");
				backButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						backButtonActionPerformed(evt);
					}
				});
			}
		}
		{
			ResulttablePanel = new JPanel();
			BorderLayout ResulttablePanelLayout = new BorderLayout();
			ResulttablePanelLayout.setHgap(10);
			ResulttablePanelLayout.setVgap(10);
			
			MainPanel.add(ResulttablePanel, BorderLayout.CENTER);
			ResulttablePanel.setLayout(ResulttablePanelLayout);
			{
				TableModel resultTableModel = 
					new NonEditableDefaultTableModel(
							//new String[][] { { "1","One","two" }, { "2","Three","four"} },
							null,
							new String[] { "id","Date","Title" });
				
				resultTable = new JTable();
				BorderLayout resultTableLayout = new BorderLayout();
				resultTableLayout.setHgap(10);
				resultTableLayout.setVgap(10);
				ResulttablePanel.add(resultTable, BorderLayout.CENTER);
				resultTable.setModel(resultTableModel);
				resultTable.setLayout(null);
			}
		}
		{
			BorderLayout buttonPanelLayout = new BorderLayout();
			buttonPanelLayout.setHgap(10);
			buttonPanelLayout.setVgap(10);

			buttonPanel = new JPanel();
			MainPanel.add(buttonPanel, BorderLayout.SOUTH);
			buttonPanel.setLayout(buttonPanelLayout);
			{
				viewJobResponseButton = new JButton();
				buttonPanel.add(viewJobResponseButton, BorderLayout.CENTER);
				viewJobResponseButton.setLayout(null);
				viewJobResponseButton.setText("View Job Response");
				viewJobResponseButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						viewJobResponseButtonActionPerformed(evt);
					}
				});
			}
		}
		{
			this.setSize(600, 400);
		}
	}

	@Override
	public void updateAvailableActions() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initialize(ViewJobResponseResultController controller, List<JobResponseDTO> jobResponses) {
		this.controller = controller;
		populateResultTable(jobResponses);
		
	}

	private void populateResultTable(List<JobResponseDTO> jobResponses) {
		DefaultTableModel model = (DefaultTableModel)this.resultTable.getModel();
		//clear table
		model.setNumRows(0);
		
		if(jobResponses==null)
			return;
		
		Iterator<JobResponseDTO> i = jobResponses.iterator();
		while(i.hasNext()){
			JobResponseDTO jobResponse = i.next();
			String[] row = new String[3];
			//id
			row[0] = jobResponse.getId();
			//date
			row[1] = DateUtils.getStringFromDate(jobResponse.getDate(),"dd-MMM-yyyy");
			//title
			row[2] = jobResponse.getTitle();
			//add to row
			model.addRow(row);
		}
	}
	
	private void viewJobResponseButtonActionPerformed(ActionEvent evt) {
		System.out.println("viewJobResponseButton.actionPerformed, event="+evt);
		
		if(resultTable.getSelectedRow()<0){
			System.out.println("No row selected");
			JOptionPane.showMessageDialog(null, Constants.ERR_SELECT_A_ROW, "Info", JOptionPane.INFORMATION_MESSAGE);
		}
		
		String id = (String)resultTable.getValueAt(resultTable.getSelectedRow(),0);
		System.out.println("id string: "+id);
		JobResponseDTO jobResponse = controller.retriveJobResponseByID(id);
		System.out.println("jobResponse:"+jobResponse);
		ViewJobResponseDetailController vjrdc = 
			new ViewJobResponseDetailController(jobResponse,this, this.getApplication());
	}
	
	private void backButtonActionPerformed(ActionEvent evt) {
		System.out.println("backButton.actionPerformed, event="+evt);
		this.controller.backToPreviousScreen();
	
	}

}
